<?php
/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * @author Damiano Ciarla
 * @package core
 * @subpackage abstract
 */
abstract class BXD_Abstract_Controller
{
	protected function params($name = null)
	{
		$params = Bxd::getInstance()->bxdConf()->params;

		if($name === null)
			return $params;
	
		if(isset($params[$name]))
			return $params[$name];

		return null;
	}

	protected function data($name = null, $value = null)
	{
		if($name === null)
			return Bxd::getInstance()->data();

		if($value === null)
			return Bxd::getInstance()->data($name);

		Bxd::getInstance()->data($name, $value);
	}

	/**
	 * Metodo che esegue un reindirizzamento del flusso del framework verso un'altro
	 * controller e action cambiando anche la view di destinazione che sarà automaticamente
	 * creata in base ai nuovi valori
	 *
	 * @param string $controller Nome del controller
	 * @param string $action Nome della action
	 */
	protected function reRoute($controller, $action)
	{
		// Imposto il controller con la prima lettera maiuscola
		$controller = ucfirst($controller);

		// Verifico se il controller su cui si vuole fare il reRoute è lo stesso che ha chiamato questo metodo
		if(ucfirst(Bxd::getInstance()->bxdConf()->controller) == $controller)
		{
			// Verifico se il metodo è presente nel controller
			if(method_exists(Bxd::getInstance()->getController(), $action))
			{
				Bxd::getInstance()->bxdConf()->controller = $controller;
				Bxd::getInstance()->bxdConf()->action = $action;
				
				call_user_func(array(Bxd::getInstance()->getController(), $action));
			}
			else throw new BXD_Exception_Controller(array($action, $controller), BXD_Exception_Controller::METHOD_NOT_EXISTS);
		}
		else
		{
			Bxd::getInstance()->reRoute($controller, $action);
		}
	}

	/**
	 * Metodo che effettua un cambio della view da includere alla fine della chiamata
	 *
	 * @param string $view Nome della view da visualizzare
	 */
	protected function changeView($view)
	{
		// Ricostruisco il path della nuova view
		$path = Bxd::getInstance()->pathApplication . '/' . Bxd::getInstance()->folderConf()->folder_view . '/' . $view . '.php';

		// Verifico che il file sia presente
		if(file_exists($path))
		{
			// Imposto la nuova view da visualizzare
			Bxd::getInstance()->bxdConf()->view = $view;
		}
		else throw new BXD_Exception_Controller($view, BXD_Exception_Controller::VIEW_NOT_EXISTS);
	}
}
?>
